#include <bits/stdc++.h>

#define ll long long
#define ull unsigned long long
#define uint unsigned int
#define endl '\n'

const int INF = 1e9;
const int MOD = 998244353;

#define YES cout << "YES" << '\n'
#define NO cout << "NO" << '\n'

using namespace std;

/*思考
贪心之左右开弓
*/
void solve() {
    int n;
    cin >> n;
    vector<pair<ll, ll>> pair(n);
    for (int i = 0; i < n; i++) {
        ll l, r;
        cin >> l >> r;
        pair[i] = {l, r};
    }
    sort(pair.begin(), pair.end());
    priority_queue<ll, vector<ll>, greater<ll>> pq;
    ll x = 0;
    ll used = 0;
    ll i = 0;
    while (true) {
        while (i < n && pair[i].first <= x) {
            pq.push(pair[i++].second);
        }

        while (!pq.empty() && pq.top() < x) {
            pq.pop();
        }
        if (pq.empty()) break;
        pq.pop();
        x++;
        used++;
        if (used == n) break;
    }
    cout << used << endl;
}
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t = 0;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}